package com.daniel.cms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daniel.cms.entity.CategoryEntity;
import com.daniel.cms.mapper.CategoryMapper;
import com.daniel.cms.utils.MapUtil;
import com.daniel.cms.utils.Mes;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 详细说明
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/4/21 10:50
 */
@Service
public class CategoryService {
	@Autowired
	CategoryMapper mapper;

	/**
	 * 根据id获取文章信息
	 *
	 * @param id id
	 * @return Mes
	 */
	public Mes info(int id) {
		try {
			if (StringUtils.isEmpty(id + "")) {
				return Mes.error("请求主键为空");
			}
			CategoryEntity entity = mapper.selectById(id);
			return entity != null ? Mes.success(entity) : Mes.error("数据不存在");
		} catch (Exception ex) {
			return Mes.error(ex.getMessage());
		}
	}

	/**
	 * 创建文章
	 *
	 * @param entity 请求实体
	 * @return Mes
	 */
	@Transactional(rollbackFor = Exception.class)
	public Mes create(CategoryEntity entity) {
		try {
			String pid = entity.getPid();
			if (!"0".equals(pid)) {
				CategoryEntity model = mapper.selectById(pid);
				if (model != null) {
					model.setHasChildren(1);
					mapper.updateById(model);

					int result = mapper.insert(entity);
					return result > 0 ? Mes.success(entity) : Mes.error("新增失败");
				} else {
					return Mes.error("父节点不存在");
				}
			} else {
				int result = mapper.insert(entity);
				return result > 0 ? Mes.success(entity) : Mes.error("新增失败");
			}
		} catch (Exception ex) {
			return Mes.error(ex.getMessage());
		}
	}

	/**
	 * 更新文章
	 *
	 * @param entity 请求实体
	 * @return Mes
	 */
	public Mes update(CategoryEntity entity) {
		try {
			CategoryEntity model = mapper.selectById(entity.getId());
			if (model == null) {
				return Mes.error("数据不存在");
			}
			int result = mapper.updateById(entity);
			return result > 0 ? Mes.success(entity) : Mes.error("更新失败");
		} catch (Exception ex) {
			return Mes.error(ex.getMessage());
		}
	}

	@Transactional(rollbackFor = Exception.class)
	public Mes delete(CategoryEntity entity) {
		try {
			QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
			List<CategoryEntity> list;
			if (entity.getPid().equals("0")) {
				queryWrapper.eq("pid", entity.getId());
				mapper.delete(queryWrapper);
				int result = mapper.deleteById(entity);
				return result > 0 ? Mes.success("删除成功") : Mes.error("删除失败");
			} else {
				queryWrapper.eq("pid", entity.getPid());
				list = mapper.selectList(queryWrapper);
				if (list.size() == 1) {
					CategoryEntity parent = mapper.selectById(entity.getPid());
					parent.setHasChildren(0);
					mapper.updateById(parent);
				}
				int result = mapper.deleteById(entity);
				return result > 0 ? Mes.success("删除成功") : Mes.error("删除失败");
			}
		} catch (Exception ex) {
			return Mes.error(ex.getMessage());
		}
	}

	/**
	 * 删除文章
	 *
	 * @param map Map
	 * @return Mes
	 */
	public Mes delete(Map<String, String> map) {
		try {
			String ids = map.get("ids");
			String[] stars = ids.split(",");
			List<String> arrays = Arrays.asList(stars);
			int result = mapper.deleteBatchIds(arrays);
			return result > 0 ? Mes.success("删除成功") : Mes.error("删除失败");
		} catch (Exception ex) {
			return Mes.error(ex.getMessage());
		}
	}

	/**
	 * 获取数据
	 *
	 * @param current 页索引
	 * @param limit   页大小
	 * @param name    用户名
	 * @param pid     父节点
	 * @return Mes
	 */
	public Mes list(int current, int limit, String pid, String name) {
		try {
			QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("pid", pid);
			if (!StringUtils.isEmpty(name)) {
				queryWrapper.like("name", name);
			}
			queryWrapper.orderByAsc("sort");
			if (current == 0 && limit == 0) {
				List<CategoryEntity> list = mapper.selectList(queryWrapper);
				return Mes.success(list);
			} else {
				Page<CategoryEntity> page = new Page<>(current, limit);
				mapper.selectPage(page, queryWrapper);
				return Mes.success(page);
			}
		} catch (Exception ex) {
			return Mes.error(ex.getMessage());
		}
	}

	/**
	 * 获取数据
	 *
	 * @param pid 父节点
	 * @return Mes
	 */
	public Mes list(String pid) {
		try {
			return Mes.success(getChildList(pid));
		} catch (Exception ex) {
			return Mes.error(ex.getMessage());
		}
	}

	private List<Map<String, Object>> getChildList(String pid) {
		// 递归调用
		try {
			List<Map<String, Object>> mapList = new ArrayList<>();
			QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("pid", pid);
			queryWrapper.eq("status", 0);
			queryWrapper.orderByAsc("sort");
			List<CategoryEntity> list = mapper.selectList(queryWrapper);
			for (CategoryEntity entity : list) {
				Map<String, Object> map = MapUtil.objToMap(entity);
				if (entity.getHasChildren() == 1) {
					List<Map<String, Object>> result = getChildList(entity.getId());
					map.put("children", result);
				}
				mapList.add(map);
			}
			return mapList;
		} catch (Exception ex) {
			return null;
		}
	}
}
